package net.chathu.airgw.model 
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.HTTPStatusEvent;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestHeader;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	/**
	 * ...
	 * @author ze
	 */
	
	[Event(name = "complete", type = "flash.events.Event")] 
	[Event(name = "ioError", type = "flash.events.IOErrorEvent")] 
	
	public class HTTPReader extends EventDispatcher
	{
		
		private var loader:URLLoader;
		private var _httpResponseData:HTTPResponseData;
		public function HTTPReader() 
		{
			
		}
		
		public function loadUrl(url:String, cookie:String = null, referer:String = null, postData:URLVariables = null):void {
			this._httpResponseData = new HTTPResponseData();
			loader = new URLLoader();
			
			var request:URLRequest = new URLRequest(url);
			
			if (postData)
			{
				request.method = URLRequestMethod.POST;
			} else {
				request.method = URLRequestMethod.GET;
			}
			
			
			//request.requestHeaders.push(new URLRequestHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; hu; rv:1.9.2.10) Gecko/20100914 Firefox/3.6.10"));
			request.requestHeaders.push(new URLRequestHeader("Accept", "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"));
			request.requestHeaders.push(new URLRequestHeader("Accept-Language", "hu-hu,hu;q=0.8,en-us;q=0.5,en;q=0.3"));
			
			//request.requestHeaders.push(new URLRequestHeader("Accept-Charset", "ISO-8859-2,utf-8;q=0.7,*;q=0.7"));
			
			if (cookie)
			{
				request.requestHeaders.push(new URLRequestHeader("Cookie", cookie));
			}
			
			/*if (referer)
			{
				request.requestHeaders.push(new URLRequestHeader("Referer", referer));
			}*/
			
			if(postData)
			{
				request.requestHeaders.push(new URLRequestHeader("Content-type", "application/x-www-form-urlencoded"));
				//connection.setRequestProperty("Content-Length", Integer.toString(ps.length()));
				request.data = postData;
			}
			
			//loader.addEventListener(ProgressEvent.PROGRESS, onProgress);
			loader.addEventListener(Event.COMPLETE, onComplete);
			loader.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
			loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
			loader.addEventListener(HTTPStatusEvent.HTTP_RESPONSE_STATUS, onResponseStatus);
			loader.load(request);
			
		}
		
		/*private function onProgress(event:ProgressEvent):void {
			//NO OP
		}*/
		
		private function onComplete(event:Event):void {
			//NO OP
			//trace(loader.data);
			//this._httpResponseData = new HTTPResponseData(loader.data);
			this._httpResponseData.data = loader.data;
			dispatchEvent(new Event(Event.COMPLETE));
			
		}
		
		private function onIOError(event:Event):void {
			onError();
		}
		private function onSecurityError(event:Event):void {
			onError();
		}
		
		private function onError():void
		{
			dispatchEvent(new IOErrorEvent(IOErrorEvent.IO_ERROR));
		}
		
		
		private function onResponseStatus(event:HTTPStatusEvent):void
		{
			var cookie:String = "";
			var arr:Array = event.responseHeaders;
			for (var i:int = 0; i < arr.length; i++)
			{
				//trace(arr[i].name + " : " + arr[i].value);
				if (arr[i].name == "Set-Cookie")
				{
					var val:String = arr[i].value;
					cookie += val.substr(0,val.indexOf(";")) + ";";
				}
				if ( arr[i].name == "Location") {
					_httpResponseData.location = arr[i].value;
				}
			}
			//trace("COOKIE:" + cookie);
			_httpResponseData.cookie = cookie;
		}
		
		public function get httpResponseData():HTTPResponseData
		{
			return _httpResponseData;
		}
		
		
	}

}